//###############
//FILE : FileStatus.java
//WRITER : Daniel Lowengrub, daniel_i_l, 320687098
//EXERCISE : oop ex3 2011
//DESCRIPTION:
//See below
//###############
package oop.ex3.filemanager;

/**
 * Stores the status of a file in the database. It contains information about
 * whether the file has finished being created, whether there's currently an open stream reading
 * from this socket, and wether new streams are allowed to be opened. 
 * 
 * The open stream counter is volatile so it's always up to date.
 * @author daniel
 */
public class FileStatus {
    private volatile int _numberOfStreams;
    private boolean _streamingPermission;
    private boolean _finishedCreation;

    /**
     * Create a new file status with default information:
     * open streams = 0.
     * streaming permission = true.
     * finished creation = true.
     */
    public FileStatus() {
        _numberOfStreams = 0;
        _streamingPermission = true;
        _finishedCreation = true;
    }


    /**
     * Get the current number of streams reading from this file.
     * @return The number of streams.
     */
    public int getNumberOfStreams() {
        return _numberOfStreams;
    }
    
    /**
     * Increment by one the number of open streams.
     */
    public void addStream() {
    	//System.out.println("adding stream: " + _numberOfStreams);
        _numberOfStreams++;
    }
    
    /**
     * Decrement by one the number of open streams.
     */
    public void removeStream() {
    	//System.out.println("removing stream: " + _numberOfStreams);
        if (_numberOfStreams > 0)
            _numberOfStreams--;
    }
    
    /**
     * Get the streaming permission. i.e are we allowed to open new streams.
     * @return True iff the streaming permission is true and the file has finished creation.
     */
    public boolean getStreamingPermission() {
        return _streamingPermission && _finishedCreation;
    }
    
    /**
     * Set the streaming permission.
     * @param permission The streaming permission.
     */
    public void setStreamingPermission(boolean permission) {
        _streamingPermission = permission;
    }

    /**
     * Set the creation status.
     * @param status True if the file has finished being created and false otherwise.
     */
    public void setCreationStatus(boolean status) {
        _finishedCreation = status;
    }
    
    /**
     * Check if the file has finished being created.
     * @return True iff the file has finished being created.
     */
    public boolean finishedCreation() {
        return _finishedCreation;
    }

}
